package com.rpa.server.servicerpaserver.crawler.controller;

import com.rpa.secure.servicerpasecure.common.constant.MessageConstant;
import com.rpa.secure.servicerpasecure.common.exception.TaskException;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.secure.servicerpasecure.common.util.CurrentTeacherInfoUtil;
import com.rpa.server.servicerpaserver.crawler.entity.User;
import com.rpa.server.servicerpaserver.crawler.operate.LogonOperate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @className: Logon
 * @author: lzt
 * @date: 2025/1/9 14:23
 * @Version: 1.0
 * @description:
 */
@Tag(name = "外系统登录相关接口", description = "LogonCrawlerController")
@Slf4j
@RestController
@RequestMapping("/crawler")
public class LogonCrawlerController {
    @Autowired
    private LogonOperate logonOperate;

    /*
     * 登录学习通获取cookie
     * */
    @PostMapping("/logInCX")
    @Operation(summary = "登录超星", description = "logInCX")
    @Parameter(name = "id", description = "本系统用户id")
    public Result<Boolean> logInCX(@RequestBody(required = true) User user) {
        // 检查 user 对象是否为空
        if (user == null) {
            log.error("请求体中的 user 对象为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }
        Long rpaRegisterId = user.getId();

        // 检查 rpaRegisterId 是否为空
        if (rpaRegisterId == null) {
            log.error("请求体中的 rpaRegisterId 字段为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }

        log.info("当前用户id为：{}", CurrentTeacherInfoUtil.getCurrentTeacherId());
        log.info("接收到的 rpaRegisterId 为：{}", rpaRegisterId);

        if (rpaRegisterId.equals(CurrentTeacherInfoUtil.getCurrentTeacherId())) {
            log.info("校验通过");
            return Result.success(logonOperate.logonCX(rpaRegisterId).join());
        } else {
            log.error("校验失败！rpaRegisterId 不匹配");
            throw new TaskException(MessageConstant.ULTRA_VIRES);
        }
    }

}
